package cn.itlaobing.eshop.dao;

import cn.itlaobing.eshop.entity.Users;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by Administrator on 2017/11/15.
 */
public class UserDao extends BaseDao{

    /**
     * 登录
     * @param userAccount
     * @param userPassWord
     * @return
     * @throws SQLException
     */
    public Users login(String userAccount,String userPassWord)throws SQLException{

        QueryRunner queryRunner=new QueryRunner(getDataSource());

        String sql="select * from users where userAccount=? and userPassWord=?";

        BeanHandler<Users> beanHandler=new BeanHandler<Users>(Users.class);

        Users us=queryRunner.query(sql,beanHandler,userAccount,userPassWord);

        return us;
    }

    /**
     * 注册信息
     * @param users
     * @return
     * @throws SQLException
     */
    public int register(Users users)throws SQLException{

        QueryRunner queryRunner=new QueryRunner(getDataSource());

        String sql="insert into users(userAccount,userPassword,userRegDateTime,userLastLoginDateTime) values(?,?,now(),now())";

        BeanHandler<Users> beanHandler=new BeanHandler<Users>(Users.class);

        int r=queryRunner.execute(sql,users.getUserAccount(),users.getUserPassword());

        return r;
    }
}
